package com.yuren.codecrushlearn.Q5300;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created with Intellij IDEA.
 * Description:
 *  不同K数和
 * @author 10204
 * @date 2025-05-28 23:49
 */
public class Q5392 {
    public List<Integer> sumKNums(int nums[], int n, int k) {
        ArrayList<Integer> integers = new ArrayList<>(k);
        sumKNums(nums, integers, 0, k, 0, 0);
        return integers.stream()
                .distinct()
                .sorted(Comparator.comparingInt(o -> o))
                .collect(Collectors.toList());
    }

    private void sumKNums(int nums[], List<Integer> path, int index, int k, int sum, int pathLen) {
        if (pathLen == k) {
            path.add(sum);
            return;
        }
        if (index == nums.length) {
            return;
        }
        sumKNums(nums, path, index + 1, k, sum, pathLen);
        sumKNums(nums, path, index + 1, k, sum + nums[index], pathLen + 1);
    }
}
